package tableMaker;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class BDTableMaker {

	public static void main(String[] args) throws IOException {
		String[] dirs = {"bdresults/dim2","bdresults/dim2", "bdresults/dim2", "bdresults/dim3", "bdresults/dim4", "bdresults/dim5"
				,"bdresults/dim6", "bdresults/dim7", "bdresults/dim8", "bdresults/dim9", "bdresults/dim10", "bdresults/dim11",
				"bdresults/dim12", "bdresults/dim13", "bdresults/dim14", "bdresults/dim15"};
		
		String[] eddirs = {"bdresults/dim2/edited/","bdresults/dim2/edited/", "bdresults/dim2/edited/", "bdresults/dim3/edited/", "bdresults/dim4/edited/", "bdresults/dim5/edited/"
				,"bdresults/dim6/edited/", "bdresults/dim7/edited/", "bdresults/dim8/edited/", "bdresults/dim9/edited/", "bdresults/dim10/edited/", "bdresults/dim11/edited/",
				"bdresults/dim12/edited/", "bdresults/dim13/edited/", "bdresults/dim14/edited/", "bdresults/dim15/edited/"};
		
		
		String[] names = new String[1000];
		String[] filename = new String[1000];
		double[] solutionsbd1 = new double[1000];
		double[] solutionsbd2 = new double[1000];

		double[] solmediansbd1 = new double[100];
		double[] solmediansbd2 = new double[100];
		int continst;
		int filecont = 0;
		FileWriter resultsTOTAL;
		FileWriter compresultsTOTAL;
		FileWriter compresultsLatexTOTAL;
				
		for(int dim = 2; dim < 10; dim++){
			filecont = 0;
			resultsTOTAL = new FileWriter(eddirs[dim]+"ResultsTOTALDim="+ Integer.toString(dim) + ".txt", false);
			compresultsTOTAL = new FileWriter(eddirs[dim]+"CompResultsTOTALDim="+ Integer.toString(dim) + ".txt", false);
			compresultsLatexTOTAL = new FileWriter(eddirs[dim]+"CompLatexResultsTOTALDim="+ Integer.toString(dim) + ".txt", false);
			
			TableTools.initializeSimpleFileBD(resultsTOTAL, dim);
			TableTools.initializeSimpleFileBD(compresultsTOTAL, dim);
			TableTools.initializeLatexFileBD(compresultsLatexTOTAL, dim);
			
			for(File file :  new File(dirs[dim]).listFiles()){
				if(file.isFile()){
				
					FileReader filein = new FileReader(file);
					BufferedReader input = new BufferedReader(filein);
					continst = TableTools.readResultFileBD(input, names, solutionsbd1, solutionsbd2);
					
					for(int i = 0; i < continst; i++){
						solmediansbd1[filecont] += solutionsbd1[i]/((double)continst);
						solmediansbd2[filecont] += solutionsbd2[i]/((double)continst);
					}
					
					filename[filecont] = file.getName();
					filecont++;
					input.close();
				}
			}
			
			
			for(int i = 0; i < filecont; i++){
				resultsTOTAL.append(filename[i]);
				compresultsTOTAL.append(filename[i]);
				compresultsLatexTOTAL.append(filename[i]);
				resultsTOTAL.append("\t" + String.format("%.0f", solmediansbd1[i]*100) + "\t " + String.format("%.0f", solmediansbd2[i]*100) );
				compresultsTOTAL.append("\t" + String.format("%.0f", solmediansbd1[i]*100) +  "\t" + String.format("%.0f", solmediansbd2[i]*100));
				compresultsLatexTOTAL.append("&" + String.format("%.0f", solmediansbd1[i]*100) + "&" + String.format("%.0f", solmediansbd2[i]*100));
				resultsTOTAL.append("\n");
				compresultsTOTAL.append("\n");
				compresultsLatexTOTAL.append("\\\\ \n");
			}
			
			resultsTOTAL.close();
			compresultsTOTAL.close();
			compresultsLatexTOTAL.append("\\end{tabular}");
			compresultsLatexTOTAL.close();
		}
	}
}
